package cn.iocoder.yudao.module.system.controller.admin.docgroup;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.docgroup.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.docgroup.DocGroupDO;
import cn.iocoder.yudao.module.system.service.docgroup.DocGroupService;

@Tag(name = "管理后台 - 知识库")
@RestController
@RequestMapping("/system/doc-group")
@Validated
public class DocGroupController {

    @Resource
    private DocGroupService docGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建知识库")
    @PreAuthorize("@ss.hasPermission('system:doc-group:create')")
    public CommonResult<Long> createDocGroup(@Valid @RequestBody DocGroupSaveReqVO createReqVO) {
        return success(docGroupService.createDocGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新知识库")
    @PreAuthorize("@ss.hasPermission('system:doc-group:update')")
    public CommonResult<Boolean> updateDocGroup(@Valid @RequestBody DocGroupSaveReqVO updateReqVO) {
        docGroupService.updateDocGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除知识库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:doc-group:delete')")
    public CommonResult<Boolean> deleteDocGroup(@RequestParam("id") Long id) {
        docGroupService.deleteDocGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得知识库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DocGroupRespVO> getDocGroup(@RequestParam("id") Long id) {
        DocGroupDO docGroup = docGroupService.getDocGroup(id);
        return success(BeanUtils.toBean(docGroup, DocGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得知识库分页")
    public CommonResult<PageResult<DocGroupRespVO>> getDocGroupPage(@Valid DocGroupPageReqVO pageReqVO) {
        PageResult<DocGroupDO> pageResult = docGroupService.getDocGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DocGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出知识库 Excel")
    @PreAuthorize("@ss.hasPermission('system:doc-group:export')")
    @OperateLog(type = EXPORT)
    public void exportDocGroupExcel(@Valid DocGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DocGroupDO> list = docGroupService.getDocGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "知识库.xls", "数据", DocGroupRespVO.class,
                        BeanUtils.toBean(list, DocGroupRespVO.class));
    }

}